package com.clkj.xiuzheng.service;

import com.clkj.xiuzheng.dao.OutStockItemMapper;
import com.clkj.xiuzheng.dao.OutStockMapper;
import com.clkj.xiuzheng.dao.ProductMapper;
import com.clkj.xiuzheng.dao.ProductStockMapper;
import com.clkj.xiuzheng.pojo.*;
import com.clkj.xiuzheng.pojo.common.OSSUtil;
import com.clkj.xiuzheng.pojo.common.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;
    @Autowired
    ProductStockMapper productStockMapper;
    @Autowired
    OutStockItemMapper outStockItemMapper;
    @Override
    public void addProduct(Product product, HttpServletRequest request)
    {
        MultipartFile file = ((MultipartRequest) request).getFile("file");
        if (file != null) {
            String img  = OSSUtil.uploadingImg(file,"xiuzheng/product");
            product.setProductImg(img);
        }
        productMapper.insertSelective(product);
    }

    @Override
    public void deleteProduct(Integer productId) {
        productMapper.deleteByPrimaryKey(productId);
    }

    @Override
    public PageInfo listProduct(Integer pageSize, Integer pageNum,Integer companyId) {
        PageHelper.startPage(pageNum,pageSize);
//        List<Product> products = productMapper.selectAllProduct();
        Example example= new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",companyId);
        example.orderBy("productId").desc();
        return  new PageInfo(productMapper.selectByExample(example));

    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateByPrimaryKey(product);
    }

    @Override
    public List<ProductInfo> queryProduct(String name) {
        List<Product> products = productMapper.queryProduct(name);
        List<ProductInfo> list = new ArrayList<>();
        if (products!=null){
            for (Product product : products){
                OutStockItem outStockItem = outStockItemMapper.selectByProductId(product.getProductId());
                ProductStock productStock = productStockMapper.selectByProductId(product.getProductId());
                ProductInfo productInfo = new ProductInfo();
                productInfo.setProductUnit(productStock.getProductUnit());
                productInfo.setProductPrice(outStockItem.getProductPrice());
                productInfo.setFormatName(product.getFormatName());
                productInfo.setProductName(product.getProductName());
                productInfo.setProductId(product.getProductId());
                productInfo.setProductImg(product.getProductImg());
                list.add(productInfo);
            }
        }
        return list;
    }

    @Override
    public ProductInfo productDetail(Integer productId) {
        //查询商品基本信息
        Product product = productMapper.selectByPrimaryKey(productId);
        ProductStock productStock = productStockMapper.selectByProductId(productId);
        //查询商品价格
        OutStockItem outStockItem = outStockItemMapper.selectByProductId(productId);
        ProductInfo productInfo =  new ProductInfo();
        productInfo.setProductUnit(productStock.getProductUnit());
        productInfo.setProductNum(productStock.getProductNum());
        productInfo.setProductPrice(outStockItem.getProductPrice());
        productInfo.setFormatName(product.getFormatName());
        productInfo.setProductName(product.getProductName());
        productInfo.setProductId(productId);
        productInfo.setProductImg(product.getProductImg());
        return productInfo;
    }

    @Override
    public List<ProductStock> listAllProduct(Integer companyId) {
        return productStockMapper.listAllProduct(companyId);
    }

    @Override
    public List<Product> listProductAll(Integer companyId) {
        Example example= new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("companyId",companyId);
        example.orderBy("productId").desc();
      return productMapper.selectByExample(example);
    }

    @Override
    public void updateProductStatus(Integer productId, Integer status) {
        Product product = new Product();
        product.setProductId(productId);
        product.setProductStatus(status);
        productMapper.updateByPrimaryKeySelective(product);
    }


}
